﻿using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
using DbLinq.Data.Linq;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
  //[Table(Name = "I_APPLICATION_INTERFACES")]
  //public class Interfaces
  //{
  //  [Column(Name = "SZINTERFACENAME")]
  //  public string interfaceName;

  //  [Column(Name = "SZCOMMODITYNAME")]
  //  public string commodityName;
  //}

  //[Table(Name = "I_DATA_INTERFACE_DEFINITION")]
  //public class DataDefinition
  //{
  //  [Column(Name = "SZATTRIBUTENAME")]
  //  public string attributeName;

  //  [Column(Name = "SZINTERNALNAME")]
  //  public string columnName;
  //}

  

  #region Line

  //[Table(Name = "LINE")]
  //public class Line
  //{
  //  [Column(Name = "TAGNO")]
  //  public string TagNo;

  //  [Column(Name = "JOB_NO")]
  //  public string JobNo;
  //}

  [Table(Name = "LINE")]
  public partial class Line : INotifyPropertyChanging, INotifyPropertyChanged
  {

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private string _tagNo;

    private string _jobNo;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnTagNoChanging(string value);
    partial void OnTagNoChanged();
    partial void OnJobNoChanging(string value);
    partial void OnJobNoChanged();
    #endregion

    public Line()
    {
      OnCreated();
    }

    [Column(Storage = "TAGNO")]
    public string TagNo
    {
      get
      {
        return this._tagNo;
      }
      set
      {
        if ((this._tagNo != value.ToString()))
        {
          this.OnTagNoChanging(value);
          this.SendPropertyChanging();
          this._tagNo = value.ToString();
          this.SendPropertyChanged("TAGNO");
          this.OnTagNoChanged();
        }
      }
    }

    //[Column(Storage = "JOB_NO", DbType = "VarChar(5)")]
    //public string JobNo
    //{
    //  get
    //  {
    //    return this._jobNo;
    //  }
    //  set
    //  {
    //    if ((this._jobNo != value))
    //    {
    //      this.OnJobNoChanging(value);
    //      this.SendPropertyChanging();
    //      this._jobNo = value;
    //      this.SendPropertyChanged("JOB_NO");
    //      this.OnJobNoChanged();
    //    }
    //  }
    //}

    public event PropertyChangingEventHandler PropertyChanging;

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void SendPropertyChanging()
    {
      if ((this.PropertyChanging != null))
      {
        this.PropertyChanging(this, emptyChangingEventArgs);
      }
    }

    protected virtual void SendPropertyChanged(String propertyName)
    {
      if ((this.PropertyChanged != null))
      {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }
  }
  #endregion

  public partial class ApplicationInterface : DbLinq.Data.Linq.DataContext
  {
    private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();

    #region Extensibility Method Definitions
    partial void OnCreated();
    partial void InsertLine(Line instance);
    partial void UpdateLine(Line instance);
    partial void DeleteLine(Line instance);
    #endregion
    //public Table<Interfaces> interfaces;
    //public Table<DataDefinition> dataDefinition;
    public Table<Line> Lines
    {
      get
      {
        return this.GetTable<Line>();
      }
    }

    public ApplicationInterface(System.Data.IDbConnection connection)
            : base(connection, new DbLinq.SqlServer.SqlServerVendor())
        {
        }

    public ApplicationInterface(System.Data.IDbConnection connection, DbLinq.Vendor.IVendor vendor)
            : base(connection, vendor)
        {
        }
    //public ApplicationInterface(IDbConnection connection) : base(connection) { }
  }
 
  public class Program
  {
    static void Main(string[] args)
    {
      try
      {
        //string connectionString = "user id=em; password=em; data source=localdb; persist security info=true";
        string connectionString = @"Data Source=.\SQLExpress; Initial Catalog=MM; User Id=mm_login; Password=mm";
        SqlConnection connection = new SqlConnection(connectionString);

        ApplicationInterface app = new ApplicationInterface(connection);
        //var appInterfaces = from appInterface in app.interfaces
        //                    select appInterface;

        //foreach (Interfaces i in appInterfaces)
        //{
        //  Console.WriteLine(i.interfaceName + " is a " + i.commodityName + " interface.");
        //}

        var lines = from line in app.Lines
                            select line.TagNo;

        foreach (var i in lines.ToList())
        {
          Console.WriteLine(i);
        }

        Console.ReadKey();
      }
      catch (Exception ex)
      {
        throw ex;
      }
    }
  }
}